unit SmallStruct.Model.TSSDBXDataModel;

interface

uses Classes,
  Provider,
  SqlExpr,
  SmallStruct.Model.TSSDataModel;

type
  TSSDBXDataModel = class(TSSDataModel)
  private
    _DataSetProvider: TDataSetProvider;
    _Query: TSQLQuery;
  protected
    procedure OpenDataSet; override;
    procedure CloseDataSet; override;
  protected
    function GetConnection: TSQLConnection;
  public
    constructor Create(AOwner: TComponent); override;
  end;

implementation

uses SmallStruct.Database.TSSDBXDatabaseDriver;

{ TSSDBXDataModel }

procedure TSSDBXDataModel.CloseDataSet;
begin
  inherited;
  _ClientDataSet.Close;
end;

constructor TSSDBXDataModel.Create(AOwner: TComponent);
begin
  inherited;

  _Query := TSQLQuery.Create(Self);

  _DataSetProvider := TDataSetProvider.Create(Self);
  _DataSetProvider.DataSet := _Query;
  _DataSetProvider.Options := _DataSetProvider.Options + [poIncFieldProps];
  _ClientDataSet.ProviderName := _DataSetProvider.Name;
end;

function TSSDBXDataModel.GetConnection: TSQLConnection;
begin
  Result := (GetDataModelContext.GetDatabaseConnection as ISSDBXDatabaseDriver).GetConnection;
end;

procedure TSSDBXDataModel.OpenDataSet;
var
  SQL: TStringList;
begin
  _Query.SQLConnection := GetConnection;

  { Parse Select SQL }

  SQL := TStringList.Create;
  ParseSQL(SQL);
  _Query.SQL.Text := SQL.Text;
  SQL.Free;

  _Query.Open;

  { Open DataSet and Init DataSet }

  InitDataSet(_Query);

  _ClientDataSet.Open;
  _Query.Close;
end;

end.
